﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gedea.Model.Entities;
using Gedea.Service;

namespace Gedea.WinForm.UserControls
{
    public partial class CompanyControl : UserControl
    {
        public List<Control> myControls = new List<Control>();
        private Delegate btnCancelDeleg, btnOkDeleg;
        private bool isEditable;

        public CompanyControl(Delegate btnCancel, Delegate btnOk, bool isEdit)
        {
            InitializeComponent();

            btnCancelDeleg = btnCancel;
            btnOkDeleg = btnOk;
            isEditable = isEdit;
        }

        private void CompanyControl_Load(object sender, EventArgs e)
        {
            LoadUser();

            myControls.Add(textBox1);
            myControls.Add(textBox2);
            myControls.Add(textBox3);
            myControls.Add(textBox4);
            

            label10.Text = CountCharacters(textBox1, 100);
            label11.Text = CountCharacters(textBox2, 16);
            label12.Text = CountCharacters(textBox3, 11);
            label13.Text = CountCharacters(textBox4, 10);
            

            ShowErrors();
        }

        private void LoadUser()
        {
            if (isEditable)
            {
                textBox1.Text = Credentials.currentUser.Name;
                textBox2.Text = Credentials.currentUser.NameForBank;
                textBox3.Text = Credentials.currentUser.Cuit;
                textBox4.Text = Credentials.currentUser.Benefit;

                button1.Text = "Editar";
            }
        }

        #region TextChanged
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ValidateFirst();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ValidateSecond();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            ValidateThird();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            ValidateFourth();
        }           

        
        #endregion

        #region ValidateTextBox
        private void ValidateFirst()
        {
            MaxLengthValidation(textBox1, 100, "Se deben ingresar por lo menos 1 caracter y hasta {0}");

            label10.Text = CountCharacters(textBox1, 100);
        }

        private void ValidateSecond()
        {
            MaxLengthValidation(textBox2, 16, "Se deben ingresar por lo menos 1 caracter y hasta {0}");

            label11.Text = CountCharacters(textBox2, 16);
        }

        private void ValidateThird()
        {
            LengthValidation(textBox3, 11, "Se deben ingresar {0} caracteres");

            label12.Text = CountCharacters(textBox3, 11);
        }

        private void ValidateFourth()
        {
            MaxLengthValidation(textBox4, 10, "Se deben ingresar por lo menos 1 caracter y hasta {0}");

            label13.Text = CountCharacters(textBox4, 10);
        }     
        

        #endregion

        #region Validation

        private void MaxLengthValidation(Control control, int ctrLength, string msg)
        {
            if (control.Text.Length > ctrLength || control.Text.Length == 0)
            {
                errorProvider1.SetError(control, string.Format(msg, ctrLength));
            }
            else
                errorProvider1.SetError(control, null);

            ShowErrors();
        }

        private void LengthValidation(Control control, int ctrLength, string msg)
        {
            if (control.Text.Length != ctrLength)
            {
                errorProvider1.SetError(control, string.Format(msg, ctrLength));
            }
            else
                errorProvider1.SetError(control, null);

            ShowErrors();
        }

        #endregion

        #region Errors

        private void ShowErrors()
        {
            string test = "";

            test += SetErrorFormat(textBox1);
            test += SetErrorFormat(textBox2);
            test += SetErrorFormat(textBox3);
            test += SetErrorFormat(textBox4);
            
            label9.Text = "Listado de errores: \n";
            label9.Text += test;

            if (test != "")
            {
                label9.Visible = true;
            }
            else
            {
                label9.Visible = false;
            }
            ValidateButton(test);
        }

        private string SetErrorFormat(Control control)
        {
            string res;

            if (errorProvider1.GetError(control) != "")
            {
                res = string.Format("- {0} \n", errorProvider1.GetError(control));
            }
            else
                res = "";
            return res;

        }

        #endregion

        #region KeyPress

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((char.IsDigit(e.KeyChar) || char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar) || char.IsSeparator(e.KeyChar))
                && !char.IsWhiteSpace(e.KeyChar) && !char.IsNumber(e.KeyChar))
            {
                MessageBox.Show("No se permiten números, caracteres de puntuación,\no símbolos en el nombre de la empresa.");
                e.Handled = true;
                return;
            }

        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Ingrese número de CUIT sin espacio ni guiones.");
                e.Handled = true;
                return;
            }
        }


        
        #endregion

        #region CommonMethods
        private string CountCharacters(Control control, int ctrLength)
        {
            return string.Format("({0}/{1})", control.Text.Length, ctrLength);
        }

        private void ValidateButton(string test)
        {
            if (test == "" && myControls.Where(x => x.Text.Length == 0).Count() == 0)
            {
                button1.Enabled = true;
            }
            else
                button1.Enabled = false;
        }
        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            btnCancelDeleg.DynamicInvoke();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btnOkDeleg.DynamicInvoke(GenerateViewUser());
        }

        private User GenerateViewUser()
        {
            User user = Credentials.currentUser ?? new User();

            user.Name = textBox1.Text;
            user.NameForBank = textBox2.Text;
            user.Cuit = textBox3.Text;
            user.Benefit = textBox4.Text;

            return user;
        }

    }
}
